package com.erp.serviceImpl.baseDataServiceImpl;

import com.erp.dao.PersonMapper;
import com.erp.pojo.Person;
import com.erp.pojo.PersonExample;
import com.erp.service.baseDataService.PersonService;
import com.erp.utils.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Author:${JayC}
 * Date:2019/1/19
 * Time:9:59
 */
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonMapper personMapper;

    Msg msg = new Msg();
    @Override
    public Msg getPerson(String name,String type, int page, int limit) {
        PersonExample example = new PersonExample();

        PersonExample.Criteria criteria= example.createCriteria();

        if (name!=null && !"".equals(name)){
            name="%"+name+"%";
            criteria.andNameLike(name);
        }
        if (type!=null&&!"".equals(type)){
            type="%"+type+"%";
            criteria.andTypeLike(type);
        }

        PageHelper.startPage(page,limit);

        List<Person> list = personMapper.selectByExample(example);

        PageInfo<Person> pagelist = new PageInfo<>(list);

        msg.setCode(0);
        msg.setCount(pagelist.getTotal());
        msg.setMsg("");
        msg.setData(pagelist.getList());

        System.out.println(msg);

        return msg;
    }

    @Override
    public void deletePerson(Long id) {

        personMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insertPerson(Person person) {
        personMapper.insertSelective(person);
    }

    @Override
    public Person getPersonById(Long id) {

        Person person = personMapper.selectByPrimaryKey(id);

        return person;
    }

    @Override
    public void updataPerson(Person person) {
        personMapper.updateByPrimaryKeySelective(person);
    }
}
